﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using INSTA.GestionEcole.DAL;

using System.Data.Objects;
using Microsoft.Ajax.Utilities;

namespace INSTA.EcoleGestion.FORMS
{
    public partial class ListePersonnesDansEtablissement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {



            if (!IsPostBack)
            {
                DropDownList1.Items.Add("");
                DropDownList1.Items.Add("110");

                BindGrid();

            }
        }

        public void BindGrid()
        {
            using (var context = new GestionInstaEntities())
            {

                var query2 = (from p in context.Etudiant
                    select new {p.ID, p.Nom, p.Prenom, p.DateDeNaissance, p.Adresse, p.Tel, p.email, p.Promo}).ToList();

                var query3 = (from p in context.Professeur

                    select new {p.ID, p.Nom, p.Prenom, p.DateDeNaissance, p.Adresse, p.Tel, p.Email, p.Promo}).ToList();



                GridView1.DataSource = query2;
                //GridView1.DataSource = query3;
                GridView1.DataBind();

            }



        }




        private void UpdateOrAddNewRecord(string ID, string Nom, string Prenom, string dateNais, string adresse,
            string Tel, string email, string promo,
            bool isUpdate)
        {
            int idEtu = Convert.ToInt32(ID);
            using (var context = new GestionInstaEntities())
            {

                if (isUpdate)
                {

                    var query = from p in context.Etudiant
                        where p.ID == idEtu
                        select p;
                    foreach (var etu in query)
                    {
                        etu.Nom = dateNais;
                        etu.Nom = Nom;
                        etu.Prenom = Prenom;
                        etu.Tel = Tel;
                        etu.email = email;
                        etu.Promo = promo;
                        etu.Adresse = adresse;
                    }


                    context.SaveChanges();

                    //Label1.Text = ID;
                }


            }
        }

        protected void EditerLigne(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;

            BindGrid();
        }

        protected void MiseAjourDansLaBase(object sender, GridViewUpdateEventArgs e)
        {

            string id = ((TextBox) GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; //ID
            string Nom = ((TextBox) GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; //nom
            string Prenom = ((TextBox) GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text; //prenom
            string dateNais = ((TextBox) GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text; //dateNaissance
            string adresse = ((TextBox) GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text; //Adresse
            string Tel = ((TextBox) GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text; //Tel
            string email = ((TextBox) GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text; //email
            string promo = ((TextBox) GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text; //promo


            UpdateOrAddNewRecord(id, Nom, Prenom, dateNais, adresse, Tel, email, promo, true);

            GridView1.EditIndex = -1;
            BindGrid();

        }

        protected void AnnulerEdition(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGrid();
        }



        private void SuppressionPersonne(string nom, string prenom /*,string dateNais,string email*/)
        {

            using (var context = new GestionInstaEntities())
            {

                Sécurité s = new Sécurité();
                var query = from p in context.Etudiant
                    where (p.Nom == nom) & (p.Prenom == prenom)
                    //& (p.DateDeNaissance==dateNais) & (p.email==email) 
                    select p;

                foreach (var etu in query)
                {
                    s.Nom = etu.Nom;
                    s.Prenom = etu.Prenom;
                    s.DateDeNaissance = etu.DateDeNaissance;
                    s.Email = etu.email;
                    context.Sécurité.Add(s);
                    context.Etudiant.Remove(etu);
                }
                context.SaveChanges();
            }

        }

        //On récupere le nom prenom date de naissance et l'email de la personne a supprimer
        protected void SupprimerLigne(object sender, GridViewDeleteEventArgs e)
        {
            //string id = GridView1.Rows[e.RowIndex].Cells[1].Text;
            string nom = GridView1.Rows[e.RowIndex].Cells[2].Text;
            string prenom = GridView1.Rows[e.RowIndex].Cells[3].Text;
            string dateNais = GridView1.Rows[e.RowIndex].Cells[4].Text;
            string email = GridView1.Rows[e.RowIndex].Cells[7].Text;
            SuppressionPersonne(nom, prenom /*,dateNais,email*/);
            BindGrid();
        }





        protected void SelectionDunAttributDsDropList(object sender, EventArgs e)
        {
            using (var context = new GestionInstaEntities())
            {

                var query2 = (from p in context.Etudiant

                    select new {p.ID, p.Nom, p.Prenom, p.DateDeNaissance, p.Adresse, p.Tel, p.email, p.Promo}).ToList();

                if (DropDownList1.SelectedItem.Text == "")
                {
                    GridView1.DataSource = query2;
                    GridView1.DataBind();

                }
                else
                {


                    var query3 = (from p in context.Etudiant
                        /* where (p.Nom == TextBoxRecherche.Text) | (p.Prenom == TextBoxRecherche.Text) | (p.Tel == TextBoxRecherche.Text)|
                                                  (p.email == TextBoxRecherche.Text) | (p.Promo==TextBoxRecherche.Text)
                                              */
                        where p.Promo == DropDownList1.SelectedItem.Text

                        select new {p.ID, p.Nom, p.Prenom, p.DateDeNaissance, p.Adresse, p.Tel, p.email, p.Promo})
                        .ToList();
                    GridView1.DataSource = query3;
                    GridView1.DataBind();
                }

            }
        }

        protected void Imprimer(object sender, EventArgs e)
        {
            
            
            string path = @"C:\Users\Sisi\Documents\" + "SISI" + ".txt";
            using (StreamWriter file = new System.IO.StreamWriter(path, true))
            {

                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    string s="";
                    // GridView1.Rows[i].Cells.Count == nombre de colonne gridview
                    for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
                        
                        s += GridView1.Rows[i].Cells[j].Text;
                    file.WriteLine(s); 
                
                }
               
            }
        }

        protected void ChangementDePage(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex + 1;
            BindGrid();
        }
    }
}









